Ext.ns("Ext.master.organization");

/**
 * 节点管理页面
 */
Ext.master.organization.Management = Ext.extend(Ext.Panel, {
    closable: true,
    layout: "border",

    initComponent: function() {
        var infoPanel = new Ext.master.organization.InfoPanel();
        var organizationTreePanel = new Ext.master.organization.OrganizationTreePanel({infoPanel: infoPanel});

        Ext.apply(this, {
            items: [
                organizationTreePanel,
                infoPanel
            ],
            tbar: new Ext.ux.StatusBar({
                defaultText: '组织结构',
                statusAlign: 'left',
                items: [
                    {iconCls:'icon-edit', text:'修改公司', functionId: 'organization/edit_company.do',handler: this.editCompany, scope: this},
                    {iconCls:'icon-add', text:'添加部门', handler: {}, scope: this},
                    {iconCls:'icon-edit', text:'修改部门', handler: {}, scope: this},
                    {iconCls:'icon-delete', text:'删除部门', handler: {}, scope: this}
                ]
            })
        });

        Ext.master.organization.Management.superclass.initComponent.call(this);
    },

    editCompany : function() {
        var editCompanyWindow = new Ext.master.organization.EditCompanyWindow();
        editCompanyWindow.show();
        editCompanyWindow.loadData();
    }

});

/**
 * 组织结构树
 */
Ext.master.organization.OrganizationTreePanel = Ext.extend(Ext.tree.TreePanel, {

    infoPanel: null,

    region: 'west',
    split: true,
    minSize: 100,
    maxSize: 500,
//    border: false,

    width: 200,
    rootVisible: false,
    lines: true,
    autoScroll: true,
    animCollapse:false,
    animate: false,
    collapseMode:'mini',

    initComponent: function() {
        Ext.apply(this, {
            loader: new Ext.tree.TreeLoader({
                url: 'organization/organization_tree.do',
                requestMethod: 'GET'
            }),
            //根节点
            root: new Ext.tree.AsyncTreeNode({
                id: 'root',
                text: '组织结构树',
                expanded:true
            })
        });
         this.on('click', this.onClick, this);
         this.loader.on('load', this.onLoad, this);

        //显示右键菜单
        /*
         this.on('contextmenu', this.onContextMenu, this);
         */
        Ext.master.organization.OrganizationTreePanel.superclass.initComponent.call(this);
    },

    onClick: function(node, e){
        if(!node.attributes.parentId) { //公司
            this.infoPanel.showCompany()
        }
        else {
            this.infoPanel.showDepartment();
        }
    },
    onLoad: function(loader, node, response) {
    	var childNode = node.firstChild;
    	if(childNode) {
    		childNode.select();
    	}
    }

});

/**
 * Card info panel
 */
Ext.master.organization.InfoPanel = Ext.extend(Ext.Panel, {
    region: 'center',
    
    layout: 'card',
    activeItem: 0,
    border: false,
    initComponent: function(){

        var companyInfoPanel = new Ext.master.organization.CompanyInfoPanel();
        var departmentInfoPanel = new Ext.master.organization.DepartmentInfoPanel();

        Ext.apply(this, {
            items : [
                companyInfoPanel,
                departmentInfoPanel
            ]
        });
        Ext.master.organization.InfoPanel.superclass.initComponent.call(this);
    },

    showCompany: function(company){
        this.getLayout().setActiveItem(0);
    },
    showDepartment: function(department){
        this.getLayout().setActiveItem(1);
    }

});

// 显示部门信息
Ext.master.organization.DepartmentInfoPanel = Ext.extend(Ext.FormPanel, {
    html : "department info panel",
    initComponent: function(){
        Ext.master.organization.DepartmentInfoPanel.superclass.initComponent.call(this);
    }

});

// 显示公司信息
Ext.master.organization.CompanyInfoPanel = Ext.extend(Ext.FormPanel, {
    layout: 'form',
    labelAlign: 'right',
    labelWidth: 60,
    frame: true,
    initComponent: function(){
        Ext.apply(this, {
            items: [
                {
                    defaults: {width: 300},
                    defaultType: 'textfield',
                    xtype: 'fieldset',
                    title: '公司信息',
                    items:[
                        {fieldLabel: '名 称', name: 'companyName', readOnly: true, cls: 'field-read-only'},
                        {fieldLabel: '地 址', name: 'companyName', readOnly: true, cls: 'field-read-only'}
                    ]
                }
            ]
        });
        Ext.master.organization.CompanyInfoPanel.superclass.initComponent.call(this);
    }
});

// 修改公司窗口
Ext.master.organization.EditCompanyWindow = Ext.extend(Ext.Window, {
    companyId: 0
});